//
//  MZHottestViewController.m
//  WHISPER
//
//  Created by lanou3g on 15/12/22.
//  Copyright © 2015年 王艳苹. All rights reserved.
//

#import "MZHottestViewController.h"
#import "MZNewestViewCell.h"
#import "YPMakeURL.h"
#import "YPNetWorking.h"
#import "MZNewestModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "YPWebViewVC.h"
@interface MZHottestViewController ()
@property (nonatomic, strong)NSMutableArray *newestArr;
@property (nonatomic, strong)NSString *total;
@end
static NSInteger num1 = 0;
@implementation MZHottestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MZNewestViewCell" bundle:nil] forCellReuseIdentifier:@"reuse"];
    self.newestArr = [NSMutableArray array];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullHeaderAction)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(pullFooterAction)];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [[YPNetWorking shareNetWorking] getDictWithURL:[[YPMakeURL shareMakeURL] readDetailURL] dict:[[YPMakeURL shareMakeURL] makeReadDetailDicWithStr:@"hot" num1:num1 num2:self.type] success:^(NSDictionary *dict) {
        self.total = (NSString *)dict[@"data"][@"total"];
        for (NSDictionary *dic in dict[@"data"][@"list"]) {
            MZNewestModel *m = [[MZNewestModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.newestArr addObject:m];
        }
        [self.tableView reloadData];
    } fail:^(NSError *err) {
        
    }];
}

- (void)pullHeaderAction
{
    
    [[YPNetWorking shareNetWorking] getDictWithURL:[[YPMakeURL shareMakeURL] readDetailURL] dict:[[YPMakeURL shareMakeURL] makeReadDetailDicWithStr:@"hot" num1:num1 num2:self.type] success:^(NSDictionary *dict) {
        [self.newestArr removeAllObjects];
        self.total = (NSString *)dict[@"data"][@"total"];
        for (NSDictionary *dic in dict[@"data"][@"list"]) {
            MZNewestModel *m = [[MZNewestModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.newestArr addObject:m];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError *err) {
        
    }];
    
}

- (void)pullFooterAction
{
    num1 += 10;
    [[YPNetWorking shareNetWorking] getDictWithURL:[[YPMakeURL shareMakeURL] readDetailURL] dict:[[YPMakeURL shareMakeURL] makeReadDetailDicWithStr:@"hot" num1:num1 num2:self.type] success:^(NSDictionary *dict) {
        self.total = (NSString *)dict[@"data"][@"total"];
        for (NSDictionary *dic in dict[@"data"][@"list"]) {
            MZNewestModel *m = [[MZNewestModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.newestArr addObject:m];
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError *err) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.newestArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MZNewestViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    MZNewestModel *m = self.newestArr[indexPath.row];
    cell.titleLabel.text = m.title;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
    cell.detailLabel.text = m.content;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 134;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MZNewestModel *model = self.newestArr[indexPath.row];
    YPWebViewVC *webVC = [[YPWebViewVC alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:webVC];
    NSString *str = [NSString stringWithFormat:@"http://pianke.me/webview/%@",model.contentID];
    webVC.tingID = str;
    webVC.type = @"读书";
    webVC.contendId = model.contentID;
    webVC.shareTitle = model.title;
    webVC.shareImage = model.coverimg;
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
